/*
 *  Copyrigh 2009 Codeinn
 *  This program is distributed under the terms of the GNU General Public License
 *
 *  This file is part of Codeinn.
 *
 *  Codeinn is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Codeinn is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Codeinn.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package http.runnable;

import codeinn.CodeinnMIDlet;
import http.DJangoConnection;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import util.Dictionary;

public class FetchPosts implements Runnable {

        private DJangoConnection dJangoConnection;
        private CodeinnMIDlet codeinnMIDlet;
        private xml.PostsHandler postsXMLHandler;

        public FetchPosts(CodeinnMIDlet codeinnMIDlet, DJangoConnection dJangoConnection) {
            this.dJangoConnection = dJangoConnection;
            this.codeinnMIDlet = codeinnMIDlet;
        }

        public void run() {
            this.postsXMLHandler = new xml.PostsHandler();
            InputStream inputStream = null;
            try {
                String url = this.codeinnMIDlet.getAppProperty("url")+this.codeinnMIDlet.getAppProperty("url.listposts");
                System.out.println(url);
                inputStream = this.dJangoConnection.httpFetchPosts(url, this.codeinnMIDlet.getCurrentThread());
                this.processOutput(inputStream);
            } catch(IOException ex) {
                ex.printStackTrace();
            } catch(ParserConfigurationException ex) {
                ex.printStackTrace();
            } catch(SAXException ex) {
                ex.getMessage();
                ex.printStackTrace();
            }
        }

        private void processOutput(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
            System.out.println("Posts downloaded, about to parse: "+inputStream);
            // Parse XML
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser saxParser = spf.newSAXParser();
            saxParser.parse(inputStream, this.postsXMLHandler);
            //System.out.println("  table: "+this.availableVideosList);
            Form formPosts = this.codeinnMIDlet.getFormPosts();
            Dictionary posts = this.getPosts();
            if(posts.isEmpty()) {
                formPosts.append(new StringItem("", this.codeinnMIDlet.getAppProperty("groups.noposts") ));
            } else {
                String[] titles = posts.keys();
                for(int i=0; i<titles.length; i++) {
                    String threadTitle = titles[i];
                    Image img = images.ImageCreator.getImage("/images/post.png");
                    //formPosts.append( new ImageItem(threadTitle, img, ImageItem.LAYOUT_RIGHT, threadTitle) );
                    formPosts.append(new StringItem(threadTitle, (String) posts.get(threadTitle)));
                }
            }
            if(inputStream != null) inputStream.close();
        }
        
        /**
         * In case of having a big list, the user might click on a item and then list will not bet
         * complete yet. So it is needed to be thread safe, because the list is retrieved because it contains
         * the video URL.
         *
         * @return
         */
        public synchronized Dictionary getPosts() {
            return this.postsXMLHandler.getPostsList();
        }

    }
    